<template>
  <el-dialog
    :close-on-click-modal="false"
    :before-close="dialogCancel"
    :visible.sync="isVisible"
    :title="title"
    width="550px"
    @open="workerSelectInit"
  >
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
      <el-form-item v-if="false" label="ID">
        <el-input v-model="form.id" style="width: 370px;" />
      </el-form-item>
      <el-form-item v-if="false" label="项目">
        <el-input v-model="form.projectId" style="width: 370px;" />
      </el-form-item>
      <el-form-item v-if="false" label="项目阶段">
        <el-input v-model="form.stageId" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="任务类型">
        <el-input v-model="form.type" :disabled="true" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="计划完成时间" prop="planFinishTime">
        <el-date-picker
          v-model="form.planFinishTime"
          type="date"
          :picker-options="pickerOptions"
          style="width: 370px;"
        />
      </el-form-item>
      <el-form-item label="项目名称">
        <el-input v-model="form.projectName" :disabled="true" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="阶段名称">
        <el-input v-model="form.stageName" :disabled="true" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="任务简要描述" prop="simpleDescribe">
        <el-input v-model="form.simpleDescribe" :rows="3" type="textarea" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="人员安排" class="member">
        <el-transfer
          v-model="workShowListRight"
          filterable
          :button-texts="['','']"
          :titles="['可选人员','已选人员']"
          :data="workShowListLeft"
        />
        <!-- <el-input v-model="form.workerNo" style="width: 370px;" /> -->
      </el-form-item>
      <el-form-item v-if="false" label="当前完成状态">
        <el-input v-model="form.workState" style="width: 370px;" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="dialogCancel">取消</el-button>
      <el-button :loading="crud.cu === 2" type="primary" @click="taskAssignSubmit(form)">确认</el-button>
    </div>
  </el-dialog>
</template>
<script>
import CRUD from '@crud/crud'
import { getWorkerList } from '@/api/employeeCenterDepartView'
import { get as getStaffWorkTask, add as addStaffList, del as delStaffList } from '@/api/staffWorkTask'
import { edit as editIssue } from '@/api/projectIssue'
import { edit as editTask } from '@/api/projectTask'
export default {
  name: 'WorkingStaffDialog',
  props: {
    isVisible: {
      type: Boolean,
      default: false
    },
    currentTaskID: {
      type: String,
      default: 't0'
    },
    crud: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      title: '任务-人员分配',
      rules: {
        planFinishTime: [
          { required: true, message: '计划完成时间不能为空', trigger: 'blur' }
        ],
        simpleDescribe: [
          { required: true, message: '任务描述不能为空', trigger: 'blur' }
        ]
      },
      form: {

      },
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date())
          }
        }, {
          text: '明天',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() + 3600 * 1000 * 24)
            picker.$emit('pick', date)
          }
        }, {
          text: '一周后',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() + 3600 * 1000 * 24 * 7)
            picker.$emit('pick', date)
          }
        }]
      },
      currentStaffInfo: {},
      workList: {},
      workShowListLeft: [],
      workShowListRight: []
    }
  },
  created() {
    this.initWorkerList()
  },
  methods: {
    initWorkerList() {
      this.form = this.crud.form
      const searchParams = { 'departmentID': 1 }
      getWorkerList(searchParams).then(res => {
        this.workList.content = res.content
        this.workList.totalElements = res.totalElements
      })
    },
    workerSelectInit() {
      var searchParams = {}
      if (this.currentTaskID.search('t') === 0) {
        // console.log('type == task')
        searchParams = { 'taskId': this.currentTaskID.replace('t', ''), page: 0, size: 9999 }
      } else if (this.currentTaskID.search('i') === 0) {
        // console.log('type == issue')
        searchParams = { 'issueId': this.currentTaskID.replace('i', ''), page: 0, size: 9999 }
      }
      this.workShowListLeft = []
      for (let i = 0; i < this.workList.totalElements; i++) {
        this.workShowListLeft.push({
          key: this.workList.content[i].id,
          label: this.workList.content[i].showName
        })
      }
      this.workShowListRight = []
      getStaffWorkTask(searchParams).then(res => {
        this.currentStaffInfo = res
        // console.log('init currentStaffInfo')
        // console.log(this.currentStaffInfo)
        for (let i = 0; i < res.totalElements; i++) {
          this.workShowListRight.push(res.content[i].workerId)
        }
        // console.log(this.workShowListRight)
      })
    },
    editStaffWorker() {
      var delInfo = []
      for (let i = 0; i < this.currentStaffInfo.totalElements; i++) {
        delInfo.push(this.currentStaffInfo.content[i].id)
      }
      return delStaffList(delInfo).then(res => {
        // console.log(res)
        // 目前分配人默认都为ID 1的用户
        var staffInfo
        for (let i = 0; i < this.workShowListRight.length; i++) {
          if (this.currentTaskID.search('t') === 0) {
            // console.log('type == task')
            staffInfo = { 'assignId': 1, 'workerId': this.workShowListRight[i], 'taskId': this.currentTaskID.replace('t', '') }
          } else if (this.currentTaskID.search('i') === 0) {
            // console.log('type == issue')
            staffInfo = { 'assignId': 1, 'workerId': this.workShowListRight[i], 'issueId': this.currentTaskID.replace('i', '') }
          }
          addStaffList(staffInfo).then(res => {
            // console.log(res)
          })
        }
      })
    },
    editTaskInfo() {
      // 可修改任务的描述与计划完成时间
      // console.log('editTaskInfo')
      // console.log(this.crud.form.id)
      const info = this.crud.form
      var taskInfo
      if (this.currentTaskID.search('t') === 0) {
        // console.log('type == task')
        taskInfo = { 'id': this.currentTaskID.replace('t', ''), 'planFinishTime': info.planFinishTime, 'sourceSection': info.simpleDescribe }
        return editTask(taskInfo).then(res => {
          // console.log(res)
        })
      } else if (this.currentTaskID.search('i') === 0) {
        // console.log('type == issue')
        taskInfo = { 'id': this.currentTaskID.replace('i', ''), 'planFinishTime': info.planFinishTime, 'issueDesc': info.simpleDescribe }
        return editIssue(taskInfo).then(res => {
          // console.log(res)
        })
      }
    },
    taskAssignSubmit(data) {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.editStaffWorker().then(() => {
            this.editTaskInfo().then(() => {
              this.crud.notify('任务分配成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
              this.$emit('dialogCancel', false)
              // this.isVisible = false
              this.crud.resetForm()
              this.crud.refresh()
            })
          })
        }
      })
    },
    dialogCancel() {
      this.crud.resetForm()
      this.$emit('dialogCancel', false)
      // this.isVisible = false
    }
  }
}
</script>

<style>
</style>
